package com.ticket.sass.admin.util;

import cn.hutool.core.collection.CollectionUtil;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author ywb
 * @date 2022-11-24 11:34
 */
public class CompareUtil<T extends Comparable<T>> {
    private final Set<T> oldSet;
    private final Set<T> newSet;
    private final List<T> addList;
    private final List<T> delList;
    private final List<T> commonList;
    
    public CompareUtil(List<T> oldList, List<T> newList) {
        oldSet = new HashSet<>(oldList);
        newSet = new HashSet<>(newList);
        commonList = new ArrayList<>(oldSet);
        commonList.retainAll(newSet);
        oldSet.removeAll(commonList);
        newSet.removeAll(commonList);
        addList = new ArrayList<>(newSet);
        delList = new ArrayList<>(oldSet);
    }
    
    public List<T> getInsert() {
        return addList;
    }
    
    public List<T> getDel() {
        return delList;
    }
    
    public Boolean isInsert(){
        return CollectionUtil.isNotEmpty(this.addList);
    }
    
    public Boolean isDel(){
        return CollectionUtil.isNotEmpty(this.delList);
    }
    
    public Boolean isCommon(){
        return CollectionUtil.isNotEmpty(this.commonList);
    }
    
    public List<T> getCommonList() {
        return commonList;
    }
    
    /**
     * 检查旧集合和新集合是否完全相同
     *
     * @return 如果两个集合完全相同返回 true，否则返回 false
     */
    public Boolean isSame() {
        return addList.isEmpty() && delList.isEmpty();
    }
    
    
    @Override
    public String toString() {
        return "CompareUtil{" +
            "oldList=" + oldSet +
            ", newList=" + newSet +
            ", addList=" + addList +
            ", delList=" + delList +
            ", commonList=" + commonList +
            '}';
    }
}
